<!--
 * @Descripttion:
 * @Author: liuxiaoling
 * @Date: 2021-03-22 19:07:54
 * @LastEditors: liuxiaoling
 * @LastEditTime: 2021-03-28 21:34:22
-->
<template>
  <div class="page_canShare">
    <van-nav-bar
      :title="navBarTitle"
      left-arrow
      @click-left="handleLeavePage"
    />

    <van-notice-bar
      :text="`当前的审核状态为：${auditStatusText} ${auditShareStatus == 2 ? `。驳回理由：${jobDetailInfo.rebutmsg || '无'}。` : '，不能修改！'}`"
      left-icon="volume-o"
      :scrollable="false"
      wrapable
      v-show="jobObj.keyword === 'auditState'"
    />

    <com-all-type-jobs
      :status="1"
      :jobType="1"
      :btnShow="true"
      :jobTypeId="jobObj.job_category_id"
      :jobDetailInfo="jobDetailInfo"
      :parentOrSelfJobId="jobObj.job_id + ''"
      :keyword="jobObj.keyword"
      @getJobContent="toPublishJob"
      :auditShareStatus="auditShareStatus+''"
    >
    </com-all-type-jobs>
  </div>
</template>

<script>
import {Dialog, Toast} from 'vant'
import comAllTypeJobs from '../../../components/comAllTypeJobs.vue'
import jobCategoryObj from '../../../constants/jobCategoryObj';
import {mapGetters} from 'vuex';
import _lodash from 'lodash-es';


export default {
  name: 'canShareAndAuditState',
  components: {
    comAllTypeJobs
  },
  data() {
    return {
      jobCategoryObj,
      jobObj: {},
      jobDetailInfo: {},
      // 审核状态
      auditShareStatus: 0,
      // 审核文本
      auditStatusText: '',
      statusObj: {
        '0': '待审',
        '1': '审核通过',
        '2': '审核失败',
      },
    }
  },

  created() {
    if (this.$route.query.jobObj) {
      this.jobObj = JSON.parse(this.$route.query.jobObj)
    } else {
      Toast.fail('出错了！')
      this.$router.back();
    };
  },

  computed: {
    ...mapGetters(['allUserInfo']),
    navBarTitle(){
      const titleObj = {
        'canShare': `转发${this.jobCategoryObj[this.jobObj.job_category_id]}`,
        'auditState': '发布职位的审核状态',
        'jobSheet': `发布收集表的招聘【${this.jobCategoryObj[this.jobObj.job_category_id]}】`,
      };

      return titleObj[this.jobObj.keyword];
    },
  },


  mounted() {
    const {keyword} = this.jobObj;
    if (keyword === 'jobSheet'){
      this._getJobDetailObj_sheet(this.jobObj.job_id);
    } else {
      this._getJobDetailObj(this.jobObj.job_id);
    }
  },

  methods: {
    async _getJobDetailObj(id) {
      const res = await this.$api.job.reqJobDetailObj(id);
      if (res.code === 200) {


        this.auditStatusText = this.statusObj[res.data.status];
        this.auditShareStatus = res.data.status;

        /*if (res.data.status == 0) res.data.status = '待审'
        if (res.data.status == 1) res.data.status = '审核通过'
        if (res.data.status == 2) res.data.status = '审核失败';*/


        const detailInfoObj = res.data;
        /*
        * todo 需要根据关键词进行校验。 （2022年4月3日18:32:18 已完成）
        *  1、keyword有: canShare、auditState 两种情况；
        * 2、是否为自己转发自己这两种情况
        *
        * */

        if( this.jobObj.keyword === 'canShare' && (detailInfoObj.creator_user_id !== this.allUserInfo.id) ){
          //  深拷贝: 深拷贝不但对指针进行拷贝，而且对指针指向的内容进行拷贝，经深拷贝后的指针是指向两个不同地址的指针。
          const newDetailObj = _lodash.cloneDeep(detailInfoObj || {} );

          // 备注项均需要删除掉
          const needRemoveKeyList = ['job_notes', 'notes_photos', 'notes_videos'];

          needRemoveKeyList.forEach((eachKey, index) => {
            delete newDetailObj[eachKey];
          });

          this.jobDetailInfo = newDetailObj;

        } else {
          this.jobDetailInfo = detailInfoObj;
        }
      }
    },

    async _getJobDetailObj_sheet(id) {
      const res = await this.$api.others.reqJobSheetDetailObj(id);
      if (res.code === 200) {

        this.auditStatusText = this.statusObj[res.data.status];
        this.auditShareStatus = res.data.status;

        this.jobDetailInfo = res.data;
      }
    },


    handleLeavePage(){
      /*Dialog.confirm({
        message: '保存此次编辑？',
        confirmButtonText: '保存',
        cancelButtonText: '不保存',
      })
        .then(() => {
          // todo 发送请求保存到数据库
          const draftObj = localStorage.getItem('draft_publish');
          this.$router.back();

        })
        .catch(() => {
          // 不报存
          this.$router.back();
        });*/
      this.$router.back();
    },

    async toPublishJob(value) {
      console.log('发布职位',  JSON.stringify(value));

      // 上传图片校验
      /*if(this.countImageNum(value.photo) == 0) {
        Toast("请先上传图片！")
        return
      }*/

      const res = await this.$api.job.reqPublishJob(value)
      if (res?.code === 200) {
        Toast('发布成功, 可在我的公司查看！')
        await this.$router.push('/myCompany')
      }

    },

    countImageNum(photos_list_url) {
      if(!photos_list_url || photos_list_url.length == 0) {
        return 0
      }
      let count = 0;
      for(let img of photos_list_url) {
        if(img) {
          count++;
        }
      }
      return count
    }
  }
}
</script>

<style scoped></style>
